package com.efounder.frame.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;

import com.efounder.constant.EnvironmentVariable;
import com.efounder.frame.bean.FilterBean;
import com.efounder.frame.bean.FilterNodeBean;
import com.efounder.frame.filter.FilterInterface;
import com.efounder.frame.utils.Constants;
import com.efounder.mobilemanager.R;
import com.efounder.util.ToastUtil;
import com.utilcode.util.ScreenUtils;
import com.utilcode.util.SizeUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;


/**
 * 过滤消息 simple
 * create by yqs
 */
public class FilterMessageFragment extends DialogFragment implements View.OnClickListener {


    private static final String TAG = "FilterMessageFragment";
    private ArrayList<FilterNodeBean> filterNodeBeans;


    private EditText etSerach;
    private Button butSearch;
    private Button butCannel;
    private int chatId;
    private byte chatType = 0;


    public FilterMessageFragment() {
    }

    @SuppressLint("ValidFragment")
    public FilterMessageFragment(int chatId) {
        this.chatId = chatId;

    }

    @Override
    public void onStart() {
        super.onStart();


        getDialog().getWindow()
                .setLayout(ScreenUtils.getScreenWidth() * 2 / 3, WindowManager.LayoutParams.WRAP_CONTENT);
        getDialog().getWindow().setGravity(Gravity.RIGHT | Gravity.TOP);
        getDialog().getWindow().getAttributes().verticalMargin = SizeUtils.dp2px(50);
        getDialog().setCanceledOnTouchOutside(true);
        getDialog().setCancelable(true);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.ef_fragment_filter_message, container, false);
        etSerach = (EditText) rootView.findViewById(R.id.et_serach);
        butSearch = (Button) rootView.findViewById(R.id.but_search);
        butCannel = (Button) rootView.findViewById(R.id.but_cannel);
        butCannel.setOnClickListener(this);
        butSearch.setOnClickListener(this);
        initData();
        return rootView;

    }

    private void initData() {
        if (getActivity().getIntent()!=null){
            this.chatType = getActivity().getIntent().getByteExtra("chattype", (byte) 0);

        }
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.but_search) {
            if (etSerach.getText().toString().trim().equals("")) {
                ToastUtil.showToast(getActivity().getApplicationContext(), "搜索关键字不能为空");
                return;
            }
            String text = etSerach.getText().toString();
            String sql1 = "select * from Message where " +
                    "( bodyString like '%" + text + "%' "
                    + " and toUserId=" + chatId
                    + " and fromUserId=" + EnvironmentVariable.getProperty(Constants.CHAT_USER_ID)
                    + " and toUserType=" + chatType + ") "
                    + " or ( bodyString like '%" + text + "%' "
                    + " and toUserId=" + EnvironmentVariable.getProperty(Constants.CHAT_USER_ID)
                    + " and fromUserId=" + chatId
                    + " and toUserType=" + chatType + ") ";

           // ToastUtil.showToast(getActivity().getApplicationContext(), "搜索sql:" + sql1);
            ((FilterInterface) getActivity()).beginFilter(sql1);
            dismiss();

        } else if (v.getId() == R.id.but_cannel) {
            FilterBean bean1 = new FilterBean();
            bean1.type = 0;
            EventBus.getDefault().post(bean1);
            dismiss();
        }
    }
}


